package com.itbour.web.action.trade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.SizeInfoDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.trade.ItemInfoDto;
import com.itbour.dto.trade.PartnerInfoDto;
import com.itbour.dto.trade.PrintParaInfoDto;
import com.itbour.service.trade.ItemsService;
import com.itbour.service.trade.PrintParaService;
import com.opensymphony.xwork2.ActionContext;

/**
 * copyright Itbour team 2016
 * 
 * @author abby
 */

public class GetPrintParaWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PrintParaService printParaService;
	
	private String delIds;
	
	// [商品]
	private HashMap<String, Object> printParaMapResult = new HashMap<String, Object>();//[分页]商品信息列表
	private List<PrintParaInfoDto> printParaList;// 印刷信息列表
	private PrintParaInfoDto printParaInfo;// 更新用
	private String ppId;// 印刷参数id
	//private List<FeedbackFromInfoDto> fbFromComboList;// 反馈来源

	private String findPricing;		// 检索条件[计价方式]
	private String findPubFlg;		// 检索条件[发布状态]
	private String findPpState;		// 检索条件[状态]
	private String findPpType;		// 检索条件[分类]
	private String findPpName;		//检索条件[印刷套餐名称]
	private String ids;				// 通用ids[逗号连接]
	private String order;			// 显示顺序

	private List<PrintParaInfoDto> printParaInfoList;
	private List<SizeInfoDto> sizeComboList;

	private Object printPara;
	
	
	// 分页获取印刷信息
	public String getPrintParaInfoPg() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(findPpName)) {
			param.put("pp_name", "%"+findPpName+"%");
		}
		if (StringUtils.isNotEmpty(findPpType)) {
			param.put("type", findPpType);
		}
		if (StringUtils.isNotEmpty(findPpState)) {
			param.put("state", findPpState);
		}
		if (StringUtils.isNotEmpty(findPricing)) {
			param.put("pricing", findPricing);
		}
		Page page = printParaService.printParaPgQuery(param,
				Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		//writeJSON(resMap);
		printParaMapResult = resMap;
		return SUCCESS;
	}
	
	// 获取印刷信息列表
	public String getPrintParaInfoLst() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("pp_id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("pp_id", id);
		}
		printParaInfoList = printParaService.selectPrintParaInfo(param);
		return SUCCESS;
	}
	
	// 添加商品信息
	public void addPrintPara() throws Exception {
		if (printParaInfo != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		printParaInfo.setUpdate_by(user.getUser_id().toString());
	    	}
	    	
	    	if(printParaInfo.getPp_id()!=null && printParaInfo.getPp_id()>0){
	    		// 更新
	    		this.updPrintPara();
	    	} else {
	    		// 新建
	    		printParaInfo.setState(10);// 编辑中
	    		printParaService.addPrintPara(printParaInfo);
	    		this.writeJSON("ok");
	    	}
		} else {
			this.writeJSON("ok");
		}
	}
	// 更新印刷信息列表
	public void updPrintPara() throws Exception {
		if (printParaInfo != null && 
			((printParaInfo.getPp_id() != null && printParaInfo.getPp_id()>0))) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	
	    	if(printParaInfo.getPp_id() != null && printParaInfo.getPp_id()>0){
		    	param.put("pp_id", printParaInfo.getPp_id());
	    	}
	    	if(StringUtils.isNotBlank(printParaInfo.getIds())){
	    		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(printParaInfo.getIds())));
	    	}
	    	if(user != null) {
	    		param.put("update_by", user.getUser_id());
	    	}
	    	if(printParaInfo.getType() != null) {
	    		param.put("type", printParaInfo.getType());
	    	}
	    	if(printParaInfo.getState() != null) {
	    		param.put("state", printParaInfo.getState());
	    	}
	    	if(printParaInfo.getPp_name() != null ){
		    	param.put("pp_name", printParaInfo.getPp_name());
	    	}
	    	if(printParaInfo.getName() != null) {
	    		param.put("name", printParaInfo.getName());
	    	}
	    	if(printParaInfo.getSize_id() != null && printParaInfo.getSize_id()>0) {
	    		param.put("size_id", printParaInfo.getSize_id());
	    	}
	    	if(printParaInfo.getStyle() != null) {
	    		param.put("style", printParaInfo.getStyle());
	    	}
	    	if(printParaInfo.getMat() != null) {
	    		param.put("mat", printParaInfo.getMat());
	    	}
	    	if(printParaInfo.getTech() != null) {
	    		param.put("tech", printParaInfo.getTech());
	    	}
	    	if(printParaInfo.getThumbs() != null) {
	    		param.put("thumbs", printParaInfo.getThumbs());
	    	}
	    	if(printParaInfo.getDesc() != null) {
	    		param.put("desc", printParaInfo.getDesc());
	    	}
	    	if(printParaInfo.getPricing() != null) {
	    		param.put("pricing", printParaInfo.getPricing());
	    	}
	    	printParaService.updPrintPara(param);
		}
		this.writeJSON("ok");
	}	

	
	//删除信息列表
	public void delPrintPara() throws Exception {
		printParaService.delPrintPara(ids);
		this.writeJSON("ok");
	}
	
	// 取尺寸列表
	public String getSizeInfoForCombo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String flg = request.getParameter("blkFlg");
		sizeComboList = printParaService.selectSizeInfo();
		if(sizeComboList != null && sizeComboList.size()>0){
			for(int i=0; i<sizeComboList.size(); i++) {
				SizeInfoDto info = sizeComboList.get(i);
				String show = info.getSize_id()
						+": W("+info.getWidth()+")*H("+info.getHeight()+")"
						+" "+info.getDesc();
				info.setComboShow(show);
			}
			
			// 添加空白项目
			if("1".equals(flg)){
				SizeInfoDto blankItem = new SizeInfoDto();
				blankItem.setSize_id(0L);;
				blankItem.setComboShow("[不限]");
				sizeComboList.add(0, blankItem);
			}
		}
		return SUCCESS;
	}
	
	public PrintParaService getPrintParaService() {
		return printParaService;
	}

	public void setPrintParaService(PrintParaService printParaService) {
		this.printParaService = printParaService;
	}

	public HashMap<String, Object> getPrintParaMapResult() {
		return printParaMapResult;
	}

	public void setPrintParaMapResult(HashMap<String, Object> printParaMapResult) {
		this.printParaMapResult = printParaMapResult;
	}

	public List<PrintParaInfoDto> getPrintParaInfoList() {
		return printParaInfoList;
	}

	public void setItemsInfoList(List<ItemInfoDto> itemsInfoList) {
		this.printParaInfoList = printParaInfoList;
	}

	public PrintParaInfoDto getPrintParaInfo() {
		return printParaInfo;
	}

	public void setPrintParaInfo(PrintParaInfoDto printParaInfo) {
		this.printParaInfo = printParaInfo;
	}

	public String getppId() {
		return ppId;
	}

	public void setItemId(String ppId) {
		this.ppId = ppId;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public String getFindPpState() {
		return findPpState;
	}

	public void setFindPpState(String findPpState) {
		this.findPpState = findPpState;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFindPpName() {
		return findPpName;
	}

	public void setFindPpName(String findPpName) {
		this.findPpName = findPpName;
	}

	public String getDelIds() {
		return delIds;
	}

	public void setDelIds(String delIds) {
		this.delIds = delIds;
	}

	public String getFindPpType() {
		return findPpType;
	}

	public void setFindPpType(String findPpType) {
		this.findPpType = findPpType;
	}

	public List<SizeInfoDto> getSizeComboList() {
		return sizeComboList;
	}

	public void setSizeComboList(List<SizeInfoDto> sizeComboList) {
		this.sizeComboList = sizeComboList;
	}

	public String getFindPricing() {
		return findPricing;
	}

	public void setFindPricing(String findPricing) {
		this.findPricing = findPricing;
	}
	
}
